package com.cardone.platform.usercenter.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.usercenter.dto.UserActivationDto;

/**
 * 用户激活
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class UserActivationJdbcDao implements UserActivationDao {
	@Override
	public int deleteByIds(final UserActivationDto deleteUserActivation) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteUserActivation.getAttrs())) {
			model.putAll(deleteUserActivation.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteUserActivation.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final UserActivationDto findUserActivation) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUserActivation, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findUserActivation, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findUserActivation.getAttrs())) {
			model.putAll(findUserActivation.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final UserActivationDto findUserActivation) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findUserActivation);

		final Model model = ModelUtils.newModel(new ModelArgs(findUserActivation, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findUserActivation.getAttrs())) {
			model.putAll(findUserActivation.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final UserActivationDto findListUserActivation) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListUserActivation).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListUserActivation.getAttrs())) {
			model.putAll(findListUserActivation.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final UserActivationDto insertUserActivation) {
		if (StringUtils.isBlank(insertUserActivation.getId())) {
			insertUserActivation.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertUserActivation));

		if (MapUtils.isNotEmpty(insertUserActivation.getAttrs())) {
			model.putAll(insertUserActivation.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final UserActivationDto paginationUserActivation) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationUserActivation).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationUserActivation.getAttrs())) {
			model.putAll(paginationUserActivation.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationUserActivation), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final UserActivationDto readUserActivation) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readUserActivation, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readUserActivation.getAttrs())) {
			model.putAll(readUserActivation.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readUserActivation);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readUserActivation, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final UserActivationDto saveUserActivation) {
		final P oldUserActivation = this.findByCodeOrId(mappedClass, saveUserActivation);

		if (oldUserActivation == null) {
			this.insertByCode(saveUserActivation);

			return this.findByCodeOrId(mappedClass, saveUserActivation);
		}

		final int updateCount = this.updateByCode(saveUserActivation, oldUserActivation);

		if (updateCount < 1) {
			return oldUserActivation;
		}

		return this.findByCodeOrId(mappedClass, saveUserActivation);
	}

	@Override
	public int updateByCode(final UserActivationDto updateUserActivation) {
		final UserActivationDto oldUserActivation = this.findByCodeOrId(UserActivationDto.class, updateUserActivation);

		return this.updateByCode(updateUserActivation, oldUserActivation);
	}

	private <P> int updateByCode(final UserActivationDto updateUserActivation, final P oldUserActivation) {
		if (oldUserActivation == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateUserActivation);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateUserActivation, oldUserActivation, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldUserActivation, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldUserActivation, updateUserActivation, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateUserActivation, updateProperties), new ModelArgs(updateUserActivation, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateUserActivation.getAttrs())) {
			model.putAll(updateUserActivation.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}